package com.itheima.boottlias.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.boottlias.domain.pojo.Student;
import com.itheima.boottlias.domain.query.QueryParam;
import com.itheima.boottlias.domain.vo.PageResult;
import com.itheima.boottlias.mapper.StudentMapper;
import com.itheima.boottlias.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author ghy
 * @version 1.0
 * @since 2025-07-15 11:15
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> pageQuery(QueryParam queryParam) {
        PageHelper.startPage(queryParam.getPage(), queryParam.getPageSize());
        Page<Student> page = studentMapper.findByCondition(queryParam);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Student student) {
        LocalDateTime now = LocalDateTime.now();
        student.setCreateTime(now);
        student.setUpdateTime(now);
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.insert(student);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        if(ArrayUtil.isNotEmpty(ids)) {
            studentMapper.deleteByIds(ids);
        }
    }

    @Override
    public Student findById(Integer id) {
        return studentMapper.findByIdAndClassName(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 违纪处理
     *
     * @param id    违纪人ID
     * @param score 扣除分数
     */
    @Override
    public void violation(Integer id, Short score) {
        studentMapper.updateViolationCountAndScore(id, score);
    }
}
